import { defineStore } from 'pinia'
import CryptoJS from 'crypto-js'
export const useStore = defineStore('token', {
  state: () => ({
    jwt: '',
    userInfo: {},
    openid: '',
    account: ''
  }),
  getters: {
    getToken(state) {
      // console.log(state.jwt)
      return state.jwt
    },
    getUserInfo(state) {
      return state.userInfo
    },
    getOpenid(state) {
      return state.openid
    },
    getAccount(state) {
      return state.account
    }
  },
  actions: {
    auth(jwt) {
      console.log(jwt)
      let arr = jwt.split('.')
      arr[1] = arr[1].replace(/-/g, '+')
      arr[1] = arr[1].replace(/_/g, '/')
      this.jwt = jwt
      let json = JSON.parse(CryptoJS.enc.Base64.parse(arr[1]).toString(CryptoJS.enc.Utf8))
      if (json['openid']) {
        this.openid = json['openid']
      }
      // if (json['account']) {
      //   this.account = json['account']
      // }
    },
    logout() {
      this.jwt = ''
      // this.userInfo = {}
    }
  },
})